package com.brsya.movie.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.brsya.base.bean.MovieListBean;
import com.brsya.base.util.GlideUtil;
import com.brsya.movie.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/7/5  16:46
 * Description:
 * 自定义影视表格
 * 最多6个
 */
public class MovieTable extends LinearLayout implements View.OnClickListener {

    private List<ConstraintLayout> boxList;
    private List<ImageView> imageList;
    private List<TextView> titleList;
    private List<TextView> infoList;

    /**
     * 影视数据
     */
    private List<MovieListBean.ContentListDTO.VideoListDTO> videoList;

    /**
     * 监听
     */
    private OnItemClickListener onItemClickListener;

    public MovieTable(Context context) {
        super(context);
        initView();
    }

    public MovieTable(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MovieTable(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        videoList = new ArrayList<>();
        boxList = new ArrayList<>();
        imageList = new ArrayList<>();
        titleList = new ArrayList<>();
        infoList = new ArrayList<>();


        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_movie_table, this, true);
        boxList.add(view.findViewById(R.id.movie_box_1));
        imageList.add(view.findViewById(R.id.iv_movie_1));
        titleList.add(view.findViewById(R.id.tv_movie_1));
        infoList.add(view.findViewById(R.id.tv_movie_info_1));
        boxList.add(view.findViewById(R.id.movie_box_2));
        imageList.add(view.findViewById(R.id.iv_movie_2));
        titleList.add(view.findViewById(R.id.tv_movie_2));
        infoList.add(view.findViewById(R.id.tv_movie_info_2));
        boxList.add(view.findViewById(R.id.movie_box_3));
        imageList.add(view.findViewById(R.id.iv_movie_3));
        titleList.add(view.findViewById(R.id.tv_movie_3));
        infoList.add(view.findViewById(R.id.tv_movie_info_3));
        boxList.add(view.findViewById(R.id.movie_box_4));
        imageList.add(view.findViewById(R.id.iv_movie_4));
        titleList.add(view.findViewById(R.id.tv_movie_4));
        infoList.add(view.findViewById(R.id.tv_movie_info_4));
        boxList.add(view.findViewById(R.id.movie_box_5));
        imageList.add(view.findViewById(R.id.iv_movie_5));
        titleList.add(view.findViewById(R.id.tv_movie_5));
        infoList.add(view.findViewById(R.id.tv_movie_info_5));
        boxList.add(view.findViewById(R.id.movie_box_6));
        imageList.add(view.findViewById(R.id.iv_movie_6));
        titleList.add(view.findViewById(R.id.tv_movie_6));
        infoList.add(view.findViewById(R.id.tv_movie_info_6));


        for (ConstraintLayout constraintLayout : boxList) {
            constraintLayout.setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View view) {
        if (onItemClickListener == null) {
            return;
        }
        int id = view.getId();
        MovieListBean.ContentListDTO.VideoListDTO videoBean = null;
        if (id == R.id.movie_box_1) {
            videoList.size();
            videoBean = videoList.get(0);
        } else if (id == R.id.movie_box_2) {
            if (videoList.size() >= 1) {
                videoBean = videoList.get(1);
            }
        } else if (id == R.id.movie_box_3) {
            if (videoList.size() >= 2) {
                videoBean = videoList.get(2);
            }
        } else if (id == R.id.movie_box_4) {
            if (videoList.size() >= 3) {
                videoBean = videoList.get(3);
            }
        } else if (id == R.id.movie_box_5) {
            if (videoList.size() >= 4) {
                videoBean = videoList.get(4);
            }
        } else if (id == R.id.movie_box_6) {
            if (videoList.size() >= 5) {
                videoBean = videoList.get(5);
            }
        }
        onItemClickListener.onItemClick(videoBean);
    }

    /**
     * 设置单个数据
     *
     * @param index     设置第几个数据
     * @param videoBean 设置的数据
     */
    private void setDataOnItem(int index, MovieListBean.ContentListDTO.VideoListDTO videoBean) {
        if (index >= 6) {
            return;
        }
        ImageView imageView = imageList.get(index);
        GlideUtil.loadRound(imageView, videoBean.getCoverUrl(), 6);
        titleList.get(index).setText(videoBean.getName());
        infoList.get(index).setText(videoBean.getDescribe());
    }

    /**
     * 获取数据列表
     *
     * @return 数据列表
     */
    public List<MovieListBean.ContentListDTO.VideoListDTO> getVideoList() {
        return videoList;
    }

    /**
     * 设置数据列表
     *
     * @param videoList 数据列表
     */
    public void setVideoList(List<MovieListBean.ContentListDTO.VideoListDTO> videoList) {
        this.videoList.clear();
        this.videoList.addAll(videoList);
        notifyData();
    }

    /**
     * 设置单个影视点击事件
     *
     * @param onItemClickListener 点击事件
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 刷新数据列表UI
     */
    public void notifyData() {
        for (int i = 0; i < 6; i++) {
            if (videoList.size() <= i) {
                boxList.get(i).setVisibility(GONE);
                continue;
            }
            boxList.get(i).setVisibility(VISIBLE);
            setDataOnItem(i, videoList.get(i));
        }
    }

    /**
     * 点击单个影视事件监听
     */
    public interface OnItemClickListener {
        /**
         * 点击单个影视事件
         *
         * @param videoBean 影视实体类
         */
        void onItemClick(MovieListBean.ContentListDTO.VideoListDTO videoBean);
    }
}
